package cn.highset.modules.base.controller.manage;

import cn.highset.common.constant.ContentKey;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.vo.Result;
import cn.highset.modules.base.entity.Content;
import cn.highset.modules.base.service.ContentService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Victor
 */
@Slf4j
@RestController
@Api(description = "富文本内容管理接口")
@RequestMapping("/highset/content")
@Transactional
public class ContentController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/getTrafficCn", method = RequestMethod.POST)
    @ApiOperation(value = "获取交通指南中文")
    public Result<Object> getTrafficCn() {
        return new ResultUtil<Object>().setData(contentService.findByType(ContentKey.TRAFFIC_CN));
    }

    @RequestMapping(value = "/updateTrafficCn", method = RequestMethod.POST)
    @ApiOperation(value = "更新交通指南中文")
    public Result<Object> updateTrafficCn(@ModelAttribute Content o) {
        o.setType(ContentKey.TRAFFIC_CN);
        return update(o);
    }


    @RequestMapping(value = "/getExhibitionOutlineCn", method = RequestMethod.POST)
    @ApiOperation(value = "获取展会概要中文")
    public Result<Object> getExhibitionOutlineCn() {
        return new ResultUtil<Object>().setData(contentService.findByType(ContentKey.EXHIBITION_OUTLINE_CN));
    }

    @RequestMapping(value = "/updateExhibitionOutlineCn", method = RequestMethod.POST)
    @ApiOperation(value = "更新展会概要中文")
    public Result<Object> updateExhibitionOutlineCn(@ModelAttribute Content o) {
        o.setType(ContentKey.EXHIBITION_OUTLINE_CN);
        return update(o);
    }

    @RequestMapping(value = "/getExhibitionOutlineEn", method = RequestMethod.POST)
    @ApiOperation(value = "获取展会概要英文")
    public Result<Object> getExhibitionOutlinen() {
        return new ResultUtil<Object>().setData(contentService.findByType(ContentKey.EXHIBITION_OUTLINE_EN));
    }

    @RequestMapping(value = "/updateExhibitionOutlineEn", method = RequestMethod.POST)
    @ApiOperation(value = "更新展会概要英文")
    public Result<Object> updateExhibitionOutlineEn(@ModelAttribute Content o) {
        o.setType(ContentKey.EXHIBITION_OUTLINE_EN);
        return update(o);
    }


    @RequestMapping(value = "/getContactUsCn", method = RequestMethod.POST)
    @ApiOperation(value = "获取联系我们中文")
    public Result<Object> getContactUsCn() {
        return new ResultUtil<Object>().setData(contentService.findByType(ContentKey.CONTACT_US_CN));
    }

    @RequestMapping(value = "/updateContactUsCn", method = RequestMethod.POST)
    @ApiOperation(value = "更新联系我们中文")
    public Result<Object> updateContactUsCn(@ModelAttribute Content o) {
        o.setType(ContentKey.CONTACT_US_CN);
        return update(o);
    }


    @RequestMapping(value = "/getContactUsEn", method = RequestMethod.POST)
    @ApiOperation(value = "获取联系我们英文")
    public Result<Object> getContactUsEn() {
        return new ResultUtil<Object>().setData(contentService.findByType(ContentKey.CONTACT_US_EN));
    }

    @RequestMapping(value = "/updateContactUsEn", method = RequestMethod.POST)
    @ApiOperation(value = "更新联系我们英文")
    public Result<Object> updateContactUsEn(@ModelAttribute Content o) {
        o.setType(ContentKey.CONTACT_US_EN);
        return update(o);
    }

    @RequestMapping(value = "/getStandCn", method = RequestMethod.POST)
    @ApiOperation(value = "获取展位图描述中文")
    public Result<Object> getStandCn() {
        return new ResultUtil<Object>().setData(contentService.findByType(ContentKey.STAND_CN));
    }

    @RequestMapping(value = "/updateStandCn", method = RequestMethod.POST)
    @ApiOperation(value = "更新展位图描述中文")
    public Result<Object> updateStandCn(@ModelAttribute Content o) {
        o.setType(ContentKey.STAND_CN);
        return update(o);
    }


    private Result<Object> update(Content o) {

        if (StrUtil.hasBlank(o.getType())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }
        String content = StrUtil.trim(o.getContent());
//        try {
//            if(content.getBytes("utf-8").length>1024){
//                return new ResultUtil<Object>().setErrorMsg("字节数超出1024个字节,英文、数字1字节，汉字3字节");
//            }
//        } catch (UnsupportedEncodingException e) {
//            return new ResultUtil<Object>().setErrorMsg("文本编码异常，请刷新重试");
//        }
        o.setContent(content);
        Content old = contentService.findByType(o.getType());
        if (old != null) {
            old.setContent(o.getContent());
            contentService.update(old);
        } else {
            old = new Content();
            old.setType(o.getType());
            old.setContent(o.getContent());
            contentService.save(old);
        }

        if (old == null) {
            return new ResultUtil<Object>().setErrorMsg("更新失败");
        }
        redisTemplate.delete("content::" + o.getType());
        return new ResultUtil<Object>().setSuccessMsg("更新成功");
    }

}
